package com.delivery.deliverysystem.service.impl;

import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.delivery.deliverysystem.pojo.DO.Client;
import com.delivery.deliverysystem.pojo.DO.Product;
import com.delivery.deliverysystem.pojo.DO.ProductCategory;
import com.delivery.deliverysystem.pojo.VO.ClientVO;
import com.delivery.deliverysystem.pojo.VO.ProductNameVO;
import com.delivery.deliverysystem.pojo.VO.ProductVO;
import com.delivery.deliverysystem.service.ClientService;
import com.delivery.deliverysystem.dao.ClientMapper;
import com.delivery.deliverysystem.util.StringUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import java.util.List;
import java.util.stream.Collectors;

/**
* @author Administrator
* @description 针对表【client(客户资料表)】的数据库操作Service实现
* @createDate 2025-01-13 15:37:40
*/
@Service
@Slf4j
public class ClientServiceImpl extends ServiceImpl<ClientMapper, Client>
    implements ClientService{
    @Autowired
    private ClientMapper clientMapper;

    @Override
    public boolean createClient(Client client) {
        if (client == null)return false;
        QueryWrapper<Client> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("client_id", client.getClientId());
        Client existingClient = clientMapper.selectOne(queryWrapper);
        if(existingClient != null) throw new RuntimeException("该客户编号已存在");
        int result = clientMapper.insert(client);
        return result > 0;
    }

    @Override
    public boolean updateClient(Client client) {
        if (client == null) return false;
        QueryWrapper<Client> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("client_id", client.getClientId());
        Client existingClient = clientMapper.selectOne(queryWrapper);
        if (existingClient == null) throw new RuntimeException("该客户编号不存在");
        int result = clientMapper.updateById(client);
        return result > 0;
    }

    @Override
    public List<ClientVO> getClientList() {
        List<Client> clients = clientMapper.selectList(null);
        if(clients!=null && !clients.isEmpty()){
            return clients.stream()
                    .map(client -> ClientVO.builder()
                            .clientId(client.getClientId())
                            .clientName(client.getClientName())
                            .build())
                    .collect(Collectors.toList());
        }
        return null;
    }


    @Override
    public List<Client> getClientByName(String name) {
        if(StringUtils.isEmpty(name)) return null;
        //可能有相同名字的客户
        QueryWrapper<Client> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("client_name",name);
        return clientMapper.selectList(queryWrapper);
    }

    @Override
    public List<Client> searchClient(String name) {
        if(StringUtils.isEmpty(name)) return new ArrayList<>();
        QueryWrapper<Client> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("client_name","%"+name.trim()+"%");
        List<Client> clients = clientMapper.selectList(queryWrapper);
        return clients;
    }
    @Override
    public List<ClientVO> getClientStatusList() {
        QueryWrapper<Client> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("status",0);
        List<Client> clients = clientMapper.selectList(queryWrapper);
        if(clients!=null && !clients.isEmpty()){
            return clients.stream()
                    .map(client -> ClientVO.builder()
                            .clientId(client.getClientId())
                            .clientName(client.getClientName())
                            .build())
                    .collect(Collectors.toList());
        }
        return null;
    }

    @Override
    public Client getClientById(String id) {
        if(id == null)return null;
        Client client = clientMapper.selectById(id);
        if(client != null){
            return Client.builder()
                    .clientId(client.getClientId())
                    .code(client.getCode())
                    .clientName(client.getClientName())
                    .fullName(client.getFullName())
                    .region(client.getRegion())
                    .clientType(client.getClientType())
                    .address(client.getAddress())
                    .postCode(client.getPostCode())
                    .telephoneNumber(client.getTelephoneNumber())
                    .fax(client.getFax())
                    .contact(client.getContact())
                    .phoneNumber(client.getPhoneNumber())
                    .salesman(client.getSalesman())
                    .payPlatform(client.getPayPlatform())
                    .department(client.getDepartment())
                    .work(client.getWork())
                    .email(client.getEmail())
                    .taxNumber(client.getTaxNumber())
                    .bank(client.getBank())
                    .bankNumber(client.getBankNumber())
                    .enablementTime(client.getEnablementTime())
                    .status(client.getStatus())
                    .homePage(client.getHomePage())
                    .englishName(client.getEnglishName())
                    .remark(client.getRemark())
                    .build();
        }
        return null;
    }
}




